package com.lagou.web.servlet;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SimplePropertyPreFilter;
import com.lagou.base.BaseServlet;
import com.lagou.pojo.Course;
import com.lagou.pojo.Course_Lesson;
import com.lagou.pojo.Course_Section;
import com.lagou.service.CourseContentService;
import com.lagou.service.impl.CourseContentServiceImpl;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;

@WebServlet("/courseContent")
public class CourseContentServlet extends BaseServlet {
    CourseContentService cs = new CourseContentServiceImpl();
    // 展示对应课程的章节与课时信息
    public void findSectionAndLessonByCourseId(HttpServletRequest request, HttpServletResponse response){
        try {
            String id = request.getParameter("course_id");

            List<Course_Section> sectionAndLessonByCourseId = cs.findSectionAndLessonByCourseId(Integer.valueOf(id));
            String result = JSON.toJSONString(sectionAndLessonByCourseId);
            System.out.println(result);
            // 把查询到的数据返回出去
            response.getWriter().print(result);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    // 根据课程id 回显课程信息
    public void findCourseByCourseId(HttpServletRequest request,HttpServletResponse response){
        String course_id = request.getParameter("course_id");
        Course course = cs.findCourseByCourseId(Integer.valueOf(course_id));
        // 返回json数据
        SimplePropertyPreFilter filter = new SimplePropertyPreFilter(Course.class,"id","course_name");
        String result = JSON.toJSONString(course, filter);
        try {
            response.getWriter().print(result);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    // 保存和修改信息
    public void saveOrUpdateSection(HttpServletRequest request,HttpServletResponse response){
        // 1.获取参数，从域对象中获取
        try {
            Map<String,Object> map =(Map) request.getAttribute("map");
            Course_Section courseSection = new Course_Section();
            //BeanUtils.populate(courseSection,map);
            BeanUtils.copyProperties(courseSection,map.get("section"));
            // 业务处理
            CourseContentService cs = new CourseContentServiceImpl();
            String result;
            // 判断是否携带id
            if (courseSection.getId()==0){
                // 新增操作
                result = cs.saveSection(courseSection);
                response.getWriter().print(result);
            }else{
                // 修改操作
                result = cs.updateSection(courseSection);
                response.getWriter().print(result);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    // 添加和修改课时
    public void saveOrUpdateLesson(HttpServletRequest request,HttpServletResponse response){
        try {
            Map<String,Object> map =(Map) request.getAttribute("map");
            Course_Lesson courseLesson = new Course_Lesson();
            //BeanUtils.populate(courseLesson,map);
            BeanUtils.copyProperties(courseLesson,map.get("lesson"));
            CourseContentService cs = new CourseContentServiceImpl();
            String result;
            if(courseLesson.getId()==0){
                // 添加课时
                String s = cs.insertLesson(courseLesson);
                response.getWriter().print(s);
            }else{
                // 修改课时
                String s = cs.updateLesson(courseLesson);
                response.getWriter().print(s);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
